﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BusinessObject;
using YamahaTuanTruc.BusinessService;
namespace YamahaTuanTruc
{
    public partial class FrmBike : Form
    {
        public FrmBike()
        {
            InitializeComponent();
        }

        private void kgvDSXe_SelectionChanged(object sender, EventArgs e)
        {
            
        }

        private void FrmBike_Load(object sender, EventArgs e)
        {
            using (var service = new BusinessServiceClient())
            {
                Xe[] lst = service.GetListBike();
                if (lst != null)
                {
                    for (int i = 0; i < lst.Length; i++)
                    {
                        kgvDSXe.Rows.Add();
                        kgvDSXe.Rows[i].Cells[0].Value = lst[i].ID;
                        kgvDSXe.Rows[i].Cells[1].Value = lst[i].Ten;
                        kgvDSXe.Rows[i].Cells[2].Value = lst[i].NgayNhanXe.ToString();
                        kgvDSXe.Rows[i].Cells[3].Value = lst[i].MauSac;
                        kgvDSXe.Rows[i].Cells[4].Value = lst[i].KhachHang.Ten;
                        kgvDSXe.Rows[i].Cells[5].Value = lst[i].NhanVien.Ten;

                    }
                }
            }
        }

        private void kgvDSXe_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Enable button if there are items shown
            this.qpBike1.qcbTaoMoi.Enabled = (kgvDSXe.RowCount > 0);
            this.qpBike1.qcbCapNhat.Enabled = (kgvDSXe.RowCount > 0);
            this.qpBike1.qcbXoa.Enabled = (kgvDSXe.RowCount > 0);

            //Binding data from datagridview to textboxes
            int selectedRowIndex = kgvDSXe.SelectedCells[0].RowIndex;
            if (kgvDSXe[0, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibID.InputBox.CueText = "";
                this.qpBike1.qcibID.InputBox.Text = kgvDSXe[0, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibID.InputBox.CueText = "";
                this.qpBike1.qcibID.InputBox.Text = "";
            }

            if (kgvDSXe[1, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibTen.InputBox.CueText = "";
                this.qpBike1.qcibTen.InputBox.Text = kgvDSXe[1, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibTen.InputBox.CueText = "";
                this.qpBike1.qcibTen.InputBox.Text = "";
            }

            if (kgvDSXe[2, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibNgayNhan.InputBox.CueText = "";
                this.qpBike1.qcibNgayNhan.InputBox.Text = kgvDSXe[2, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibNgayNhan.InputBox.CueText = "";
                this.qpBike1.qcibNgayNhan.InputBox.Text = "";
            }

            if (kgvDSXe[3, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibMauSac.InputBox.CueText = "";
                this.qpBike1.qcibMauSac.InputBox.Text = kgvDSXe[3, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibMauSac.InputBox.CueText = "";
                this.qpBike1.qcibMauSac.InputBox.Text = "";
            }
            if (kgvDSXe[4, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibKhachHang.InputBox.CueText = "";
                this.qpBike1.qcibKhachHang.InputBox.Text = kgvDSXe[4, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibKhachHang.InputBox.CueText = "";
                this.qpBike1.qcibKhachHang.InputBox.Text = "";
            }
            if (kgvDSXe[5, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibNhanVien.InputBox.CueText = "";
                this.qpBike1.qcibNhanVien.InputBox.Text = kgvDSXe[5, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibNhanVien.InputBox.CueText = "";
                this.qpBike1.qcibNhanVien.InputBox.Text = "";
            }
        }
        private void bindData(int selectedRowIndex)
        {
            if (kgvDSXe[0, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibID.InputBox.CueText = "";
                this.qpBike1.qcibID.InputBox.Text = kgvDSXe[0, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibID.InputBox.CueText = "";
                this.qpBike1.qcibID.InputBox.Text = "";
            }

            if (kgvDSXe[1, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibTen.InputBox.CueText = "";
                this.qpBike1.qcibTen.InputBox.Text = kgvDSXe[1, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibTen.InputBox.CueText = "";
                this.qpBike1.qcibTen.InputBox.Text = "";
            }

            if (kgvDSXe[2, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibNgayNhan.InputBox.CueText = "";
                this.qpBike1.qcibNgayNhan.InputBox.Text = kgvDSXe[2, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibNgayNhan.InputBox.CueText = "";
                this.qpBike1.qcibNgayNhan.InputBox.Text = "";
            }

            if (kgvDSXe[3, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibMauSac.InputBox.CueText = "";
                this.qpBike1.qcibMauSac.InputBox.Text = kgvDSXe[3, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibMauSac.InputBox.CueText = "";
                this.qpBike1.qcibMauSac.InputBox.Text = "";
            }
            if (kgvDSXe[4, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibKhachHang.InputBox.CueText = "";
                this.qpBike1.qcibKhachHang.InputBox.Text = kgvDSXe[4, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibKhachHang.InputBox.CueText = "";
                this.qpBike1.qcibKhachHang.InputBox.Text = "";
            }
            if (kgvDSXe[5, selectedRowIndex].Value != null)
            {
                this.qpBike1.qcibNhanVien.InputBox.CueText = "";
                this.qpBike1.qcibNhanVien.InputBox.Text = kgvDSXe[5, selectedRowIndex].Value.ToString();
            }
            else
            {
                this.qpBike1.qcibNhanVien.InputBox.CueText = "";
                this.qpBike1.qcibNhanVien.InputBox.Text = "";
            }
        }

        private void kgvDSXe_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Up || e.KeyChar == (char)Keys.Down)
            {
                DataGridViewRow selectedRow = kgvDSXe.CurrentRow;
                int selectedRowIndex = selectedRow.Index;
                bindData(selectedRowIndex);
            }
        }

        private void kgvDSXe_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                int selectedRowIndex = kgvDSXe.CurrentRow.Index;
                if (selectedRowIndex >= 0 && selectedRowIndex < kgvDSXe.RowCount - 1)
                {
                    bindData(selectedRowIndex + 1);
                }
            }
            if (e.KeyCode == Keys.Up)
            {
                int selectedRowIndex = kgvDSXe.CurrentRow.Index;
                if (selectedRowIndex >= 0 && selectedRowIndex < kgvDSXe.RowCount)
                {
                    if (selectedRowIndex == 0)
                    {
                        bindData(0);
                        return;
                    }
                    bindData(selectedRowIndex - 1);
                }
            }
        }
    }
}
